@page "/credentials/{id:guid}/delete"
@inherits MainBase
@inject CredentialService CredentialService
@using Microsoft.Extensions.Localization

<LayoutPageTitle>@Localizer["DeleteCredentialPageTitle"]</LayoutPageTitle>

<PageHeader
    BreadcrumbItems="@BreadcrumbItems"
    Title="@Localizer["DeleteCredentialTitle"]"
    Description="@Localizer["DeleteCredentialDescription"]">
</PageHeader>

@if (IsLoading)
{
    <LoadingIndicator />
}
else
{
    <div class="mx-4 p-4 mb-4 bg-white border border-gray-200 rounded-lg shadow-sm 2xl:col-span-2 dark:border-gray-700 sm:p-6 dark:bg-gray-800">
        <AlertMessageError HasTopMargin="false" Message="@Localizer["DeleteWarningMessage"]" />
        <h3 class="mt-4 mb-4 text-xl font-semibold dark:text-white">@Localizer["CredentialEntrySection"]</h3>
        <div class="mb-4">
            <label class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">@Localizer["IdLabel"]</label>
            <div class="text-gray-900 dark:text-white">@Id</div>
        </div>
        <div class="mb-4">
            <label class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">@Localizer["ServiceNameLabel"]</label>
            <div class="text-gray-900 dark:text-white">@Obj?.Service.Name</div>
        </div>
        <button @onclick="DeleteConfirm" class="text-white bg-red-600 hover:bg-red-800 focus:ring-4 focus:ring-red-300 font-medium rounded-lg text-sm inline-flex items-center px-3 py-2.5 text-center mr-2 dark:focus:ring-red-900">
            @Localizer["YesImSureButton"]
        </button>

        <button @onclick="Cancel" class="text-gray-900 bg-white hover:bg-gray-100 focus:ring-4 focus:ring-primary-300 border border-gray-200 font-medium inline-flex items-center rounded-lg text-sm px-3 py-2.5 text-center dark:bg-gray-800 dark:text-gray-400 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-700 dark:focus:ring-gray-700" data-drawer-hide="drawer-delete-product-default">
            @Localizer["NoCancelButton"]
        </button>
    </div>
}

@code {
    private IStringLocalizer Localizer => LocalizerFactory.Create("Pages.Main.Credentials.Delete", "AliasVault.Client");

    /// <summary>
    /// Gets or sets the login ID.
    /// </summary>
    [Parameter]
    public Guid Id { get; set; }

    private bool IsLoading { get; set; } = true;
    private Credential? Obj { get; set; }

    /// <inheritdoc />
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        BreadcrumbItems.Add(new BreadcrumbItem { Url = "credentials/" + Id, DisplayName = Localizer["ViewCredentialBreadcrumb"] });
        BreadcrumbItems.Add(new BreadcrumbItem { DisplayName = Localizer["DeleteCredentialBreadcrumb"] });
    }

    /// <inheritdoc />
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        if (firstRender)
        {
            // Load existing Obj, retrieve from service
            Obj = await CredentialService.LoadEntryAsync(Id);

            // Hide loading spinner
            IsLoading = false;

            // Force re-render invoke so the charts can be rendered
            StateHasChanged();
        }
    }

    private async void DeleteConfirm()
    {
        if (Obj is null)
        {
            GlobalNotificationService.AddErrorMessage(Localizer["DeleteCredentialNotFoundError"], true);
            return;
        }

        GlobalLoadingSpinner.Show(Localizer["DeletingCredentialMessage"]);
        if (await CredentialService.SoftDeleteEntryAsync(Id))
        {
            GlobalNotificationService.AddSuccessMessage(Localizer["DeleteSuccessMessage"]);
        }
        else {
            GlobalNotificationService.AddErrorMessage(Localizer["DeleteDatabaseError"], true);
        }

        GlobalLoadingSpinner.Hide();
        NavigationManager.NavigateTo("/credentials");
    }

    private void Cancel()
    {
        NavigationManager.NavigateTo("/credentials/" + Id);
    }
}
